package com.whoami.idmagic.api;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Table;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.logging.Logger;

@Component
public class HBaseConnection implements InitializingBean {

    private Logger logger = Logger.getLogger(HBaseConnection.class.getName());

    @Value("${hbase.zookeeper.addresses}")
    private String addresses;

    @Value("${hbase.zookeeper.port}")
    private String port;

    private static Configuration config = null;
    private Connection connection = null;

    public static Configuration getConfig() {
        return config;
    }

    public Connection getConnection() {
        return connection;
    }

    @Override
    public void afterPropertiesSet()  throws Exception{
        System.setProperty("hadoop.home.dir", "/usr/lib/hadoop");
        try {
            config = HBaseConfiguration.create();// 配置
            config.set("hbase.zookeeper.quorum", addresses);// zookeeper地址
            config.set("hbase.zookeeper.property.clientPort", port);// zookeeper端口
            config.set("hbase.rpc.timeout", "200000");
            config.set("hbase.client.operation.timeout", "300000");
            config.set("hbase.client.scanner.timeout.period", "1000000");

            connection = ConnectionFactory.createConnection(config);
            Admin admin = connection.getAdmin();
            logger.info("创建HBase连接成功");
        }catch (Exception e) {
            logger.info("创建HBase连接失败");
            e.printStackTrace();
        }

    }
}
